package weak_20241227_29.stream;

import java.util.ArrayList;
import java.util.function.ToIntFunction;

/**
 * @author zdl
 * @date 2024/12/27
 */
public class Demo1 {
    public static void main(String[] args) {
        //1. 计算整数列表的总和
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        int sum = list.stream().mapToInt(new ToIntFunction<Integer>() {
            @Override
            public int applyAsInt(Integer value) {
                return value;
            }
        }).sum();
        System.out.println(sum);
        /*Integer reduce = list.stream().reduce(0, new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        });*/
        Integer reduce = list.stream().reduce(0, (i1, i2) -> i1 + i2);
        System.out.println(reduce);

    /*    int[] arr = {1,2,4,5};
        System.out.println(Arrays.stream(arr).sum());*/
    }
}
